package com.mdp.im.core.publish.action.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdp.im.core.entity.ImGroup;
import com.mdp.im.core.entity.ImGroupUser;
import com.mdp.im.core.publish.action.AbstractPublishAction;
import com.mdp.im.core.publish.api.action.PublishAction;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.publish.api.StorePublisher;
import com.mdp.im.core.service.ImGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 离开群组处理器
 * 
 * @author chenyc
 * @date 2018/6/12
 */
@Service("pushLeaveChannelGroupAction")
public class LeaveChannelGroupAction extends AbstractPublishAction implements PublishAction {

    @Autowired
	ImGroupService groupService;
	
	@Autowired
	Publisher publisher;


    @Autowired
    StorePublisher storePublisher;
	
	
    /**
     * 成员离开群
     * 1.发送群通知
     * 2.将人员从数据库 group_user表移除
     * 3.将人员从服务器组移除
     * 
     * @param message
     * message:{ 
     *  action:'leaveChannelGroup',
     *  msgType:'group',  
     *  sendUserid:'发送者编号',
     *  sendUsername:'发送者姓名', 
     *  cuserid:'群主',
     * 	groupId:'群组编号不能为空',
     *  groupName:'群组名称不能为空',
     *  users:{
     *  	userid:'成员编号'
     *  	username:'成员名称'
     *  }
     * }
     * 
     * 
     */
    @Override
    public void doAction(JSONObject message) {
        JSONObject json = message;
        if (!json.containsKey("users")) {
            return;
        } 
        String groupId=json.getString("groupId"); 
         
        ImGroup groupDb=groupService.getImGroupByGroupId(groupId);
        if(groupDb!=null) {
         	message.put("groupName", groupDb.getGroupName()); 
            message.put("categoryId", groupDb.getCategoryId());
            message.put("cuserid", groupDb.getCuserid());
            message.put("cusername", groupDb.getCusername());
            message.put("allowJoin", groupDb.getAllowJoin());
            message.put("allowSpeechTime", groupDb.getAllowSpeechTime()); 
            message.put("gstatus", groupDb.getGstatus());
            
        }
         
        
        List<String> userids=new ArrayList<>();
        JSONArray jsonUsers=json.getJSONArray("users");
        List<ImGroupUser> groupUsers=new ArrayList<>(); 
        for (Object user : jsonUsers) {
        	JSONObject jsonUser=(JSONObject)user;
        	String userid=jsonUser.getString("userid");
        	String username=jsonUser.getString("username"); 
        	ImGroupUser groupUser=new ImGroupUser();  
        	groupUser.setUserid(userid);
        	groupUser.setUsername(username);
        	groupUser.setGroupId(groupId);  
        	groupUser.setStatus("4");
        	groupUsers.add(groupUser);
        	userids.add(userid); 
        } 
        groupService.asyncBatchLeaveGroupByGroupIdAndUserids(groupId,groupUsers);
        
        String msg=message.toJSONString();
        publisher.publishGroupMessage(groupId, msg);
        storePublisher.publishGroupMessageToDbQueue(msg);
        //handleMessageWithGroup(message, false, json.getString("groupId")); 
        
    }

}
